package com.suzhang.notemessage.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.Random;


/**
 * @author  suzhang
 * @version 1.0
 */

public class AlySmsUtils {

    private static final String AccessKeyId = "LTAI4FyfLBnr459VioCAA5V8";//你的accessKeyId
    private static final String AccessKeySecret = "RheC8F6dLxvWvdwyThiKx4lJnkE9OA";//你的accessKeySecret
    private static final String SignName = "黑马旅游";//使用的签名
    private static final String TemplateCode = "SMS_198931813";//发送短信使用的模板
    private static IAcsClient acs = null;//服务对象
    private static SendSmsRequest req = new SendSmsRequest();//短信发送请求对象
    static {
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKeyId, AccessKeySecret);
        acs = new DefaultAcsClient(profile);
    }
    //随机生成指定位数验证码
    public static StringBuffer randomCode(int number){
        //验证码内容集
        final char[] CHARS = {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuffer stringBuffer=new StringBuffer();
        Random r=new Random();
        for(int i=0;i<number;i++){
            stringBuffer.append(CHARS[r.nextInt(CHARS.length)]);
        }
        return stringBuffer;
    }
    //自定义发送方法
    public static boolean sendCode(String mobile, String code) throws ClientException {
        req.setPhoneNumbers(mobile);//设置接收短信手机号
        req.setSignName(SignName);//设置使用签名
        req.setTemplateCode(TemplateCode);//设置使用通知模板id
        req.setTemplateParam("{\"code\":\"" + code + "\"}");//设置请求参数 以json字符串形式与模板一致
        SendSmsResponse res = acs.getAcsResponse(req);//向服务器发送请求
        System.out.println("控制台打印res code: " + res.getCode());//响应状态码
         System.out.println("控制台打印res message: " + res.getMessage());//响应信息
        if (res.getCode() == null && !res.getCode().equals("OK")) {
            System.out.println("res.getMessage得日志:" + res.getMessage());
            return false;
        }
        return true;
    }
    public static void main(String[] args) throws ClientException {
        System.out.println(sendCode("17635065357","Youaremybaby"));
    }

}

